package angels.client.Activities;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import angels.client.Entities.Categories;
import angels.client.Entities.Categories.Type;
import angels.client.Listeners.MainMenuListener;
import angels.client.Services.BusyHourService;

public class CategoryMenu extends Activity implements MainMenuListener, ServiceConnection{
	 
	private BusyHourService service = null;
    ListView spinSubcategory_list,spinCategory_list;
    Handler handler = new Handler();
    String strSelctedCategory;
    TextView txtViewHeader;
    
    public void onServiceConnected(ComponentName className, IBinder binder) {
		service = ((BusyHourService.LocalBinder) binder).getService();
	}

	public void onServiceDisconnected(ComponentName className) {
	    service = null;
	}
	
    @Override	
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        spinCategory_list= new ListView(this);
        //spinCategory_list.setBackgroundResource(R.drawable.custom_spinner_style);
        
        final Bundle extras = getIntent().getExtras();
        
        spinCategory_list.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,int pos, long id) {
				strSelctedCategory = parent.getItemAtPosition(pos).toString();
				service.getCategories(CategoryMenu.this,Type.subcategory,strSelctedCategory);
			}
		});
        
        LinearLayout layout = new LinearLayout(this);
        layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
        layout.setOrientation(LinearLayout.VERTICAL);
        layout.setBackgroundColor(0xFFEEEEEE);
        
        txtViewHeader = new TextView(this);
        txtViewHeader.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
        txtViewHeader.setGravity(Gravity.CENTER_HORIZONTAL);
        txtViewHeader.setText("Select Category");
        txtViewHeader.setTextColor(0xFF333333);
        txtViewHeader.setTextSize(24);
        txtViewHeader.setShadowLayer((float) 0.5, 2, 2, 0xFFEEEEEE);
        txtViewHeader.setBackgroundColor(0xFFCCCCCC);
        layout.addView(txtViewHeader);
        
        layout.addView(spinCategory_list);
        layout.setClickable(false);
        layout.setFocusable(false);
        setContentView(layout);
        
        String[] categories = extras.getStringArray("categories");
        
        Intent ServiceGps = new Intent(this, BusyHourService.class);
        getApplicationContext().bindService(ServiceGps, this, Context.BIND_AUTO_CREATE);
        
        ArrayAdapter<String> catArrayAdapter = new ArrayAdapter<String>(this,
        		android.R.layout.select_dialog_item,categories);
        spinCategory_list.setAdapter(catArrayAdapter);
        //spinCategory_list.setBackgroundColor(Color.LTGRAY);
    }
    
    @Override
	public void onCategoriesReceived(Categories subcategories1) {
    	Intent intent= new Intent(getApplicationContext(), SubCategoryMenu.class);
		intent.putExtra("subcategory", subcategories1.getCategories());
		intent.putExtra("category", strSelctedCategory);
	  	startActivity(intent);	
	  	
	  	/*
    	String[] subcategories=subcategories1.getCategories();
    	final ArrayAdapter<String> subArrayAdapter = new ArrayAdapter<String>(this,
                android.R.layout.select_dialog_item,subcategories);
    	handler.post(new Runnable() {	
            public void run() {
            	spinSubcategory_list.setAdapter(subArrayAdapter);
            	//spinSubcategory_list.setSelection(0);
            }
        });*/
	}
	
	public void onCategoriesReceivedError()
	{
	}
    
	@Override
	public void onBackPressed() {
	}
}